home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / fish / 726-750 / 741 / rkrm_lib1 / rkrm_lib1.lha / GadTools / gadtoolsgadgets.c < prev    next >
C/C++ Source or Header  |  1992-09-03  |  14KB  |  431 lines

  1. ;/* gadtoolsgadgets.c - Compiled with SAS C v5.10b
  2. lc -b1 -cfistq -v -y -j73 gadtoolsgadgets
  3. blink FROM LIB:c.o gadtoolsgadgets.o TO gadtoolsgadgets LIB LIB:lc.lib LIB:amiga.lib
  4. quit
  5. */
  6.  
  7. /*
  8. Copyright (c) 1992 Commodore-Amiga, Inc.
  9.  
  10. This example is provided in electronic form by Commodore-Amiga, Inc. for
  11. use with the "Amiga ROM Kernel Reference Manual: Libraries", 3rd Edition,
  12. published by Addison-Wesley (ISBN 0-201-56774-1).
  13.  
  14. The "Amiga ROM Kernel Reference Manual: Libraries" contains additional
  15. information on the correct usage of the techniques and operating system
  16. functions presented in these examples.  The source and executable code
  17. of these examples may only be distributed in free electronic form, via
  18. bulletin board or as part of a fully non-commercial and freely
  19. redistributable diskette.  Both the source and executable code (including
  20. comments) must be included, without modification, in any copy.  This
  21. example may not be published in printed form or distributed with any
  22. commercial product.  However, the programming techniques and support
  23. routines set forth in these examples may be used in the development
  24. of original executable software products for Commodore Amiga computers.
  25.  
  26. All other rights reserved.
  27.  
  28. This example is provided "as-is" and is subject to change; no
  29. warranties are made.  All use is at your own risk. No liability or
  30. responsibility is assumed.
  31. */
  32.  
  33. /* gadtoolsgadgets.c
  34. ** Simple example of using a number of gadtools gadgets.
  35. */
  36. #define INTUI_V36_NAMES_ONLY
  37.  
  38. #include <exec/types.h>
  39. #include <intuition/intuition.h>
  40. #include <intuition/gadgetclass.h>
  41. #include <libraries/gadtools.h>
  42.  
  43. #include <clib/exec_protos.h>
  44. #include <clib/graphics_protos.h>
  45. #include <clib/intuition_protos.h>
  46. #include <clib/gadtools_protos.h>
  47.  
  48. #include <stdio.h>
  49.  
  50. #ifdef LATTICE
  51. int CXBRK(void)    { return(0); }  /* Disable Lattice CTRL/C handling */
  52. int chkabort(void) { return(0); }  /* really */
  53. #endif
  54.  
  55. /* Gadget defines of our choosing, to be used as GadgetID's,
  56. ** also used as the index into the gadget array my_gads[].
  57. */
  58. #define MYGAD_SLIDER    (0)
  59. #define MYGAD_STRING1   (1)
  60. #define MYGAD_STRING2   (2)
  61. #define MYGAD_STRING3   (3)
  62. #define MYGAD_BUTTON    (4)
  63.  
  64. /* Range for the slider: */
  65. #define SLIDER_MIN  (1)
  66. #define SLIDER_MAX (20)
  67.  
  68. struct TextAttr Topaz80 = { "topaz.font", 8, 0, 0, };
  69.  
  70. struct Library      *IntuitionBase;
  71. struct Library      *GfxBase;
  72. struct Library      *GadToolsBase;
  73.  
  74. /* Print any error message.  We could do more fancy handling (like
  75. ** an EasyRequest()), but this is only a demo.
  76. */
  77. void errorMessage(STRPTR error)
  78. {
  79. if (error)
  80.     printf("Error: %s\n", error);
  81. }
  82.  
  83. /*
  84. ** Function to handle a GADGETUP or GADGETDOWN event.  For GadTools gadgets,
  85. ** it is possible to use this function to handle MOUSEMOVEs as well, with
  86. ** little or no work.
  87. */
  88. VOID handleGadgetEvent(struct Window *win, struct Gadget *gad, UWORD code,
  89.     WORD *slider_level, struct Gadget *my_gads[])
  90. {
  91. switch (gad->GadgetID)
  92.     {
  93.     case MYGAD_SLIDER:
  94.         /* Sliders report their level in the IntuiMessage Code field: */
  95.         printf("Slider at level %ld\n", code);
  96.         *slider_level = code;
  97.         break;
  98.     case MYGAD_STRING1:
  99.         /* String gadgets report GADGETUP's */
  100.         printf("String gadget 1: '%s'.\n",
  101.                 ((struct StringInfo *)gad->SpecialInfo)->Buffer);
  102.         break;
  103.     case MYGAD_STRING2:
  104.         /* String gadgets report GADGETUP's */
  105.         printf("String gadget 2: '%s'.\n",
  106.                 ((struct StringInfo *)gad->SpecialInfo)->Buffer);
  107.         break;
  108.     case MYGAD_STRING3:
  109.         /* String gadgets report GADGETUP's */
  110.         printf("String gadget 3: '%s'.\n",
  111.                 ((struct StringInfo *)gad->SpecialInfo)->Buffer);
  112.         break;
  113.     case MYGAD_BUTTON:
  114.         /* Buttons report GADGETUP's (button resets slider to 10) */
  115.         printf("Button was pressed, slider reset to 10.\n");
  116.         *slider_level = 10;
  117.         GT_SetGadgetAttrs(my_gads[MYGAD_SLIDER], win, NULL,
  118.                             GTSL_Level, *slider_level,
  119.                             TAG_END);
  120.         break;
  121.     }
  122. }
  123.  
  124.  
  125. /*
  126. ** Function to handle vanilla keys.
  127. */
  128. VOID handleVanillaKey(struct Window *win, UWORD code,
  129.     WORD *slider_level, struct Gadget *my_gads[])
  130. {
  131. switch (code)
  132.     {
  133.     case 'v':
  134.         /* increase slider level, but not past maximum */
  135.         if (++*slider_level > SLIDER_MAX)
  136.             *slider_level = SLIDER_MAX;
  137.         GT_SetGadgetAttrs(my_gads[MYGAD_SLIDER], win, NULL,
  138.                             GTSL_Level, *slider_level,
  139.                             TAG_END);
  140.         break;
  141.     case 'V':
  142.         /* decrease slider level, but not past minimum */
  143.         if (--*slider_level < SLIDER_MIN)
  144.             *slider_level = SLIDER_MIN;
  145.         GT_SetGadgetAttrs(my_gads[MYGAD_SLIDER], win, NULL,
  146.                             GTSL_Level, *slider_level,
  147.                             TAG_END);
  148.         break;
  149.     case 'c':
  150.     case 'C':
  151.         /* button resets slider to 10 */
  152.         *slider_level = 10;
  153.         GT_SetGadgetAttrs(my_gads[MYGAD_SLIDER], win, NULL,
  154.                             GTSL_Level, *slider_level,
  155.                             TAG_END);
  156.         break;
  157.     case 'f':
  158.     case 'F':
  159.         ActivateGadget(my_gads[MYGAD_STRING1], win, NULL);
  160.         break;
  161.     case 's':
  162.     case 'S':
  163.         ActivateGadget(my_gads[MYGAD_STRING2], win, NULL);
  164.         break;
  165.     case 't':
  166.     case 'T':
  167.         ActivateGadget(my_gads[MYGAD_STRING3], win, NULL);
  168.         break;
  169.     }
  170. }
  171.  
  172.  
  173. /*
  174. ** Here is where all the initialization and creation of GadTools gadgets
  175. ** take place.  This function requires a pointer to a NULL-initialized
  176. ** gadget list pointer.  It returns a pointer to the last created gadget,
  177. ** which can be checked for success/failure.
  178. */
  179. struct Gadget *createAllGadgets(struct Gadget **glistptr, void *vi,
  180.     UWORD topborder, WORD slider_level, struct Gadget *my_gads[])
  181. {
  182. struct NewGadget ng;
  183. struct Gadget *gad;
  184.  
  185. /* All the gadget creation calls accept a pointer to the previous gadget, and
  186. ** link the new gadget to that gadget's NextGadget field.  Also, they exit
  187. ** gracefully, returning NULL, if any previous gadget was NULL.  This limits
  188. ** the amount of checking for failure that is needed.  You only need to check
  189. ** before you tweak any gadget structure or use any of its fields, and finally
  190. ** once at the end, before you add the gadgets.
  191. */
  192.  
  193. /* The following operation is required of any program that uses GadTools.
  194. ** It gives the toolkit a place to stuff context data.
  195. */
  196. gad = CreateContext(glistptr);
  197.  
  198. /* Since the NewGadget structure is unmodified by any of the CreateGadget()
  199. ** calls, we need only change those fields which are different.
  200. */
  201. ng.ng_LeftEdge   = 140;
  202. ng.ng_TopEdge    = 20+topborder;
  203. ng.ng_Width      = 200;
  204. ng.ng_Height     = 12;
  205. ng.ng_GadgetText = "_Volume:   ";
  206. ng.ng_TextAttr   = &Topaz80;
  207. ng.ng_VisualInfo = vi;
  208. ng.ng_GadgetID   = MYGAD_SLIDER;
  209. ng.ng_Flags      = NG_HIGHLABEL;
  210.  
  211. my_gads[MYGAD_SLIDER] = gad = CreateGadget(SLIDER_KIND, gad, &ng,
  212.                     GTSL_Min,         SLIDER_MIN,
  213.                     GTSL_Max,         SLIDER_MAX,
  214.                     GTSL_Level,       slider_level,
  215.                     GTSL_LevelFormat, "%2ld",
  216.                     GTSL_MaxLevelLen, 2,
  217.                     GT_Underscore,    '_',
  218.                     TAG_END);
  219.  
  220. ng.ng_TopEdge   += 20;
  221. ng.ng_Height     = 14;
  222. ng.ng_GadgetText = "_First:";
  223. ng.ng_GadgetID   = MYGAD_STRING1;
  224. my_gads[MYGAD_STRING1] = gad = CreateGadget(STRING_KIND, gad, &ng,
  225.                     GTST_String,   "Try pressing",
  226.                     GTST_MaxChars, 50,
  227.                     GT_Underscore, '_',
  228.                     TAG_END);
  229.  
  230. ng.ng_TopEdge   += 20;
  231. ng.ng_GadgetText = "_Second:";
  232. ng.ng_GadgetID   = MYGAD_STRING2;
  233. my_gads[MYGAD_STRING2] = gad = CreateGadget(STRING_KIND, gad, &ng,
  234.                     GTST_String,   "TAB or Shift-TAB",
  235.                     GTST_MaxChars, 50,
  236.                     GT_Underscore, '_',
  237.                     TAG_END);
  238.  
  239. ng.ng_TopEdge   += 20;
  240. ng.ng_GadgetText = "_Third:";
  241. ng.ng_GadgetID   = MYGAD_STRING3;
  242. my_gads[MYGAD_STRING3] = gad = CreateGadget(STRING_KIND, gad, &ng,
  243.                     GTST_String,   "To see what happens!",
  244.                     GTST_MaxChars, 50,
  245.                     GT_Underscore, '_',
  246.                     TAG_END);
  247.  
  248. ng.ng_LeftEdge  += 50;
  249. ng.ng_TopEdge   += 20;
  250. ng.ng_Width      = 100;
  251. ng.ng_Height     = 12;
  252. ng.ng_GadgetText = "_Click Here";
  253. ng.ng_GadgetID   = MYGAD_BUTTON;
  254. ng.ng_Flags      = 0;
  255. gad = CreateGadget(BUTTON_KIND, gad, &ng,
  256.                     GT_Underscore, '_',
  257.                     TAG_END);
  258. return(gad);
  259. }
  260.  
  261. /*
  262. ** Standard message handling loop with GadTools message handling functions
  263. ** used (GT_GetIMsg() and GT_ReplyIMsg()).
  264. */
  265. VOID process_window_events(struct Window *mywin,
  266.     WORD *slider_level, struct Gadget *my_gads[])
  267. {
  268. struct IntuiMessage *imsg;
  269. ULONG imsgClass;
  270. UWORD imsgCode;
  271. struct Gadget *gad;
  272. BOOL terminated = FALSE;
  273.  
  274. while (!terminated)
  275.     {
  276.     Wait (1 << mywin->UserPort->mp_SigBit);
  277.  
  278.     /* GT_GetIMsg() returns an IntuiMessage with more friendly information for
  279.     ** complex gadget classes.  Use it wherever you get IntuiMessages where
  280.     ** using GadTools gadgets.
  281.     */
  282.     while ((!terminated) &&
  283.            (imsg = GT_GetIMsg(mywin->UserPort)))
  284.         {
  285.         /* Presuming a gadget, of course, but no harm...
  286.         ** Only dereference this value (gad) where the Class specifies
  287.         ** that it is a gadget event.
  288.         */
  289.         gad = (struct Gadget *)imsg->IAddress;
  290.  
  291.         imsgClass = imsg->Class;
  292.         imsgCode = imsg->Code;
  293.  
  294.         /* Use the toolkit message-replying function here... */
  295.         GT_ReplyIMsg(imsg);
  296.  
  297.         switch (imsgClass)
  298.             {
  299.             /*  --- WARNING --- WARNING --- WARNING --- WARNING --- WARNING ---
  300.             ** GadTools puts the gadget address into IAddress of IDCMP_MOUSEMOVE
  301.             ** messages.  This is NOT true for standard Intuition messages,
  302.             ** but is an added feature of GadTools.
  303.             */
  304.             case IDCMP_GADGETDOWN:
  305.             case IDCMP_MOUSEMOVE:
  306.             case IDCMP_GADGETUP:
  307.                 handleGadgetEvent(mywin, gad, imsgCode, slider_level, my_gads);
  308.                 break;
  309.             case IDCMP_VANILLAKEY:
  310.                 handleVanillaKey(mywin, imsgCode, slider_level, my_gads);
  311.                 break;
  312.             case IDCMP_CLOSEWINDOW:
  313.                 terminated = TRUE;
  314.                 break;
  315.             case IDCMP_REFRESHWINDOW:
  316.                 /* With GadTools, the application must use GT_BeginRefresh()
  317.                 ** where it would normally have used BeginRefresh()
  318.                 */
  319.                 GT_BeginRefresh(mywin);
  320.                 GT_EndRefresh(mywin, TRUE);
  321.                 break;
  322.             }
  323.         }
  324.     }
  325. }
  326.  
  327. /*
  328. ** Prepare for using GadTools, set up gadgets and open window.
  329. ** Clean up and when done or on error.
  330. */
  331. VOID gadtoolsWindow(VOID)
  332. {
  333. struct TextFont *font;
  334. struct Screen   *mysc;
  335. struct Window   *mywin;
  336. struct Gadget   *glist, *my_gads[4];
  337. void            *vi;
  338. WORD            slider_level = 5;
  339. UWORD           topborder;
  340.  
  341. /* Open topaz 8 font, so we can be sure it's openable
  342. ** when we later set ng_TextAttr to &Topaz80:
  343. */
  344. if (NULL == (font = OpenFont(&Topaz80)))
  345.     errorMessage( "Failed to open Topaz 80");
  346. else
  347.     {
  348.     if (NULL == (mysc = LockPubScreen(NULL)))
  349.         errorMessage( "Couldn't lock default public screen");
  350.     else
  351.         {
  352.         if (NULL == (vi = GetVisualInfo(mysc, TAG_END)))
  353.             errorMessage( "GetVisualInfo() failed");
  354.         else
  355.             {
  356.             /* Here is how we can figure out ahead of time how tall the  */
  357.             /* window's title bar will be:                               */
  358.             topborder = mysc->WBorTop + (mysc->Font->ta_YSize + 1);
  359.  
  360.             if (NULL == createAllGadgets(&glist, vi, topborder,
  361.                                          slider_level, my_gads))
  362.                 errorMessage( "createAllGadgets() failed");
  363.             else
  364.                 {
  365.                 if (NULL == (mywin = OpenWindowTags(NULL,
  366.                         WA_Title,     "GadTools Gadget Demo",
  367.                         WA_Gadgets,   glist,      WA_AutoAdjust,    TRUE,
  368.                         WA_Width,       400,      WA_MinWidth,        50,
  369.                         WA_InnerHeight, 140,      WA_MinHeight,       50,
  370.                         WA_DragBar,    TRUE,      WA_DepthGadget,   TRUE,
  371.                         WA_Activate,   TRUE,      WA_CloseGadget,   TRUE,
  372.                         WA_SizeGadget, TRUE,      WA_SimpleRefresh, TRUE,
  373.                         WA_IDCMP, IDCMP_CLOSEWINDOW | IDCMP_REFRESHWINDOW |
  374.                             IDCMP_VANILLAKEY | SLIDERIDCMP | STRINGIDCMP |
  375.                             BUTTONIDCMP,
  376.                         WA_PubScreen, mysc,
  377.                         TAG_END)))
  378.                     errorMessage( "OpenWindow() failed");
  379.                 else
  380.                     {
  381.                     /* After window is open, gadgets must be refreshed with a
  382.                     ** call to the GadTools refresh window function.
  383.                     */
  384.                     GT_RefreshWindow(mywin, NULL);
  385.  
  386.                     process_window_events(mywin, &slider_level, my_gads);
  387.  
  388.                     CloseWindow(mywin);
  389.                     }
  390.                 }
  391.             /* FreeGadgets() even if createAllGadgets() fails, as some
  392.             ** of the gadgets may have been created...If glist is NULL
  393.             ** then FreeGadgets() will do nothing.
  394.             */
  395.             FreeGadgets(glist);
  396.             FreeVisualInfo(vi);
  397.             }
  398.         UnlockPubScreen(NULL, mysc);
  399.         }
  400.     CloseFont(font);
  401.     }
  402. }
  403.  
  404.  
  405. /*
  406. ** Open all libraries and run.  Clean up when finished or on error..
  407. */
  408. void main(void)
  409. {
  410. if (NULL == (IntuitionBase = OpenLibrary("intuition.library", 37)))
  411.     errorMessage( "Requires V37 intuition.library");
  412. else
  413.     {
  414.     if (NULL == (GfxBase = OpenLibrary("graphics.library", 37)))
  415.         errorMessage( "Requires V37 graphics.library");
  416.     else
  417.         {
  418.         if (NULL == (GadToolsBase = OpenLibrary("gadtools.library", 37)))
  419.             errorMessage( "Requires V37 gadtools.library");
  420.         else
  421.             {
  422.             gadtoolsWindow();
  423.  
  424.             CloseLibrary(GadToolsBase);
  425.             }
  426.         CloseLibrary(GfxBase);
  427.         }
  428.     CloseLibrary(IntuitionBase);
  429.     }
  430. }
  431.